<?php
/* [DoYouHaoBaby!] (C)Dianniu From 2010.
   Gtk树条目($)*/

!defined('DYHB_PATH') && exit;

// 导入迭代器接口
Package::importI('IIterator','MODE');

class GtkTreeItemIterator implements IIterator{

	private $_nLevel=-1;
	private $_oStartItem;
	private $_oModel;
	private $_oCurrentChildItem;
	private $_oCurrentChildIterator;
	const DONT_CHILDREN=0; // 不历遍子项
	const NO_LIMIT=-1; // 历遍所有子项

	public function __construct(GtkTreeModel $oModel,GtkTreeIter $oStartItem=null,$nLevel=0){
		$this->_oModel=$oModel;
		$this->_oStartItem=$oStartItem;
		$this->_nLevel=$nLevel;
		$this->first();
	}

	public function first(){
		$this->_oCurrentChildItem=$this->_oModel->iter_children($this->_oStartItem);
		$this->createChildIterator();
		return $this->current();
	}

	public function next(){
		if($this->isDone()){
			return null;
		}
		$oRet=$this->_oCurrentChildIterator->next();// 优先返回子迭代器
		if($oRet===null){// 子迭代器 已空，迭代到下一个 item
			$oRet=$this->_oCurrentChildItem;
			$this->_oCurrentChildItem=$this->_oModel->iter_next($this->_oCurrentChildItem);
			$this->createChildIterator();
			return $oRet;
		}
		return $oRet;
	}

	public function current(){
		if(!$this->_oCurrentChildIterator->isDone()){
			return $this->_oCurrentChildIterator->current();
		}
		else{
			return $this->_oCurrentChildItem;
		}
	}

	public function isDone(){
		return($this->_oCurrentChildItem===null);
	}

	private function createChildIterator(){
		if(!$this->_oCurrentChildItem  or  $this->_nLevel==self::DONT_CHILDREN){
			$this->_oCurrentChildIterator=new DNullIterator();
		}
		else{
			$this->_oCurrentChildIterator=new self($this->_oModel,$this->_oCurrentChildItem,$this->_nLevel-1);
		}
	}

	public function GetModel(){
		return $this->_oModel;
	}

}
